package com.doubleClick
{
	import com.eshangrao.controls.CheckboxDataGrid;
	import com.eshangrao.controls.CheckboxDataGrid_inlineComponent2;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import mx.collections.ListCollectionView;
	import mx.controls.Alert;
	import mx.controls.dataGridClasses.DataGridColumn;
	import mx.controls.dataGridClasses.DataGridItemRenderer;
	import mx.controls.listClasses.IDropInListItemRenderer;
	import mx.controls.listClasses.IListItemRenderer;
	import mx.events.DataGridEvent;
	import mx.events.ListEvent;

	/**
	 *  DataGrid that only allows editing if you double click
	 */
	public class DoubleClickCheckDataGrid extends CheckboxDataGrid
	{
		public function DoubleClickCheckDataGrid()
		{
			super();
			doubleClickEnabled=true;
		}

		override protected function selectItem(item:IListItemRenderer, shiftKey:Boolean, ctrlKey:Boolean, transition:Boolean=true):Boolean
		{
			//Check to see if the select is from the Checkbox row, all the others will have a listData property
			if (!(item as Object).hasOwnProperty("listData") && !shiftKey)
			{
				ctrlKey=true;
			}
			if (item is CheckboxDataGrid_inlineComponent2)
				return super.selectItem(item, shiftKey, ctrlKey, transition);
			return false;
		}

		// fake all keyboard interaction as if it had the ctrl key down
		override protected function keyDownHandler(event:KeyboardEvent):void
		{
			// this is technically illegal, but works
			event.ctrlKey=true;
			event.shiftKey=false;
			super.keyDownHandler(event);
		}

		override public function headerCheckboxClicked(event:Event):void
		{
			if(!allowMultipleSelection){
				return;
			}
			//Either select or deselect all the rows
			if (allItemsSelected())
			{
				//Clear all selected items
				selectedItems = new Array();
				this.dispatchEvent(new Event(ListEvent.CHANGE));
				this.dispatchEvent(new Event("clearAll"));
			}
			else
			{
				//Select the entire grid			
				if (dataProvider is ListCollectionView)
				{
					var everything:Array = new Array();
					for( var i:int = 0; i < dataProvider.length; i++ ){
						everything[i] = i;
					}
					selectedIndices = everything;
					this.dispatchEvent(new Event(ListEvent.CHANGE));
					this.dispatchEvent(new Event("selectAll"));
				}
			}
		}

		override protected function mouseDoubleClickHandler(event:MouseEvent):void
		{
			var dataGridEvent:DataGridEvent;
			var r:IListItemRenderer;
			var dgColumn:DataGridColumn;
			r = mouseEventToItemRenderer(event);
			if (r && r != itemEditorInstance && !(r is CheckboxDataGrid_inlineComponent2))
			{
				var dilr:IDropInListItemRenderer=IDropInListItemRenderer(r);
				if (columns[dilr.listData.columnIndex].editable)
				{
					dgColumn=columns[dilr.listData.columnIndex];
					dataGridEvent=new DataGridEvent(DataGridEvent.ITEM_EDIT_BEGINNING, false, true);
					// ITEM_EDIT events are cancelable
					dataGridEvent.columnIndex=dilr.listData.columnIndex;
					dataGridEvent.dataField=dgColumn.dataField;
					dataGridEvent.rowIndex=dilr.listData.rowIndex + verticalScrollPosition;
					dataGridEvent.itemRenderer=r;
					dispatchEvent(dataGridEvent);
				}
			}
			super.mouseDoubleClickHandler(event);
		}

		override protected function mouseUpHandler(event:MouseEvent):void
		{
			var r:IListItemRenderer;
			var dgColumn:DataGridColumn;
			r = mouseEventToItemRenderer(event);		 
			if (r && (r is DataGridItemRenderer)&&!(r is CheckboxDataGrid_inlineComponent2))
			{
				var dilr:IDropInListItemRenderer=IDropInListItemRenderer(r);
				if (columns[dilr.listData.columnIndex].editable)
				{
					dgColumn=columns[dilr.listData.columnIndex];
					dgColumn.editable=false;
				}
			}
			super.mouseUpHandler(event);

			if (dgColumn)
				dgColumn.editable=true;
		}
	}
}